package com.wyp168.gulimall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.wyp168.gulimall.product.vo.SpuSaveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wyp168.gulimall.product.entity.SpuInfoEntity;
import com.wyp168.gulimall.product.service.SpuInfoService;
import com.wyp168.common.utils.PageUtils;
import com.wyp168.common.utils.R;



/**
 * spu信息
 *
 * @author wyp168
 * @email 2721255240@qq.com
 * @date 2022-10-22 12:09:09
 */
@RestController
@Slf4j
@RequestMapping("product/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;






    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryPageByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVO spuSaveVO){
		spuInfoService.saveSupInfo(spuSaveVO);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 商品上架服务
     * @param spuId
     * @return
     */
    @PostMapping("/{spuId}/up")
    public R supUp(@PathVariable Long spuId) {
        try {
            spuInfoService.up(spuId);
            log.info("商品上架成功");
            return R.ok();
        } catch (Exception e) {
            log.error("商品上架异常, e:{}", e.getMessage());
            return R.error("商品上架失败");
        }
    }

    @PostMapping("testSave")
    public R testSave(@RequestBody SpuInfoEntity supInfoEntity) {
        spuInfoService.testSave(supInfoEntity);
        return R.ok();
    }

}
